package com.sxkf.template.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sxkf.core.api.vo.R;
import com.sxkf.template.entity.Post;
import com.sxkf.template.entity.PostLike;
import com.sxkf.template.entity.Topic;
import com.sxkf.template.entity.User;
import com.sxkf.template.service.PostLikeService;
import com.sxkf.template.service.PostService;
import com.sxkf.template.service.TopicService;
import com.sxkf.template.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 帖子点赞 前端控制器
 * </p>
 *
 * @author 杨玉龙
 * @since 2024-08-20 11:48:57
 */
@Api(tags = "动态（帖子）点赞")
@RestController
@RequestMapping("/postLike")
public class PostLikeController {

    @Resource
    private PostLikeService postLikeService;

    @Resource
    private PostService postService;

    @Resource
    private UserService userService;

    @Resource
    private TopicService topicService;


    @ApiOperation(value = "点赞")
    @PostMapping(value = "/add")
    public R<?> add(@RequestBody PostLike postLike){
        Boolean res = postLikeService.add(postLike);
        return R.success(res);
    }

    @ApiOperation(value = "是否点赞")
    @GetMapping(value = "/isAdd")
    public R<?> isAdd(@RequestParam String userId,
                      @RequestParam String postId){
        Boolean res = postLikeService.isAdd(userId, postId);
        return R.success(res);
    }

    @ApiOperation(value = "分页查点赞列表")
    @GetMapping(value = "/page")
    public R<IPage<Post>> page(@RequestParam Integer pageNo,
                         @RequestParam Integer pageSize,
                         @RequestParam String userId){

        Page<PostLike> page = postLikeService.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<PostLike>()
                .eq(PostLike::getUserId, userId));
        Page<Post> postPage = new Page<>(pageNo, pageSize);
        if (page.getTotal() == 0){
            return R.success(postPage);
        }
        List<String> postIdList = page.getRecords().stream().map(PostLike::getPostId).collect(Collectors.toList());
        List<Post> postList = postService.listByIds(postIdList);
        BeanUtils.copyProperties(page, postPage);


        List<String> topicIdList = postList.stream().map(Post::getTopicId).collect(Collectors.toList());

        List<Topic> topicList = topicService.listByIds(topicIdList);
        Map<String, String> topicTitleMap = topicList.stream().collect(Collectors.toMap(Topic::getId, Topic::getTitle));

        List<String> userIdList = postList.stream().map(Post::getUserId).collect(Collectors.toList());
        List<User> userList = userService.listByIds(userIdList);
        Map<String, User> userMap = userList.stream().collect(Collectors.toMap(User::getId, user -> user));

        postList.stream().forEach(post -> {
            User user = userMap.get(post.getUserId());
            post.setTopicTitle(topicTitleMap.get(post.getTopicId()));
            post.setAvatar(user.getAvatar());
            post.setNickName(user.getNickName());
        });

        postPage.setRecords(postList);



        return R.success(postPage);
    }


}
